//
// Saru.Math.plane3.cs: Plane class in 3d space
//
// Author:
//   Jan Van Sweevelt (vansweej@gmail.com)
//
// Copyright (C) 2007
// 

using System;

namespace Saru.Base.Math {

// The plane is represented as a unit length normal vector and a constant

    class Plane3f {

	public Plane3f() {
	    normal = new Vector3f();
	}

	public Plane3f(Plane3f P) {
	    normal = P.normal;
	    constant = P.constant;
	}

	public Plane3f(Vector3f V, float C) {
	    normal = V;
	    constant = C;
	}

	public Plane3f(Vector3f V1, Vector3f V2, Vector3f V3) {
	    Vector3f edge1 = V2 - V1;
	    Vector3f edge2 = V3 - V1;
	    normal = edge1.UnitCross(edge2);
	    constant = normal.Dot(V1);
	}

	public float DistanceTo(Vector3f V) {
	    return normal.Dot(V) - constant;
	}

	public int WhichSide(Vector3f V) {
	    float distance = DistanceTo(V);
	    if(distance < 0.0f)
		return -1;
	    if(distance > 0.0f)
		return 1;
	    return 0;
	}

	private Vector3f normal;
	float constant;
    }

}
